package com.kerrykidz.scmstock.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.kerrykidz.scminvoicing.dao.mapper.ScmTakeStkEntityMapper;
import com.kerrykidz.scminvoicing.dao.mapper.ScmTakestkPrdEntityMapper;
import com.kerrykidz.scminvoicing.dao.model.ScmTakeStkEntity;
import com.kerrykidz.scminvoicing.dao.model.ScmTakestkPrdEntity;
import com.kerrykidz.scmstock.service.IStockTakingService;
import com.rdp.framework.exception.RDPException;

/**
 * 
 * @ClassName: StockTakingServiceImpl
 * @description: 库存盘点服务类
 * @author: huanjiang
 * @date: 2014年9月4日 下午3:18:41
 * @version: V1.0
 * 
 */
@Service
public class StockTakingServiceImpl implements IStockTakingService {

	@Autowired
	// 库存盘点记录表
	private ScmTakeStkEntityMapper scmTakeStkEntityMapper;

	@Autowired
	// 库存盘点明细表
	private ScmTakestkPrdEntityMapper scmTakestkPrdEntityMapper;

	/**
	 * 
	 * @Title: findScmTakeStk
	 * @Description: 库存盘点 从库存盘点记录表中获取分页数据
	 * @param scmTakeStk
	 * @throws RDPException
	 * @date: 2014年9月1日
	 * @return:List<ScmTakeStkEntity>
	 * @throws
	 */
	public ScmTakeStkEntity findScmTakeStk(String startSearchTime,
			String endSearchTime, ScmTakeStkEntity scmTakeStk)
			throws RDPException {
		scmTakeStk.setIspaging(true);
		Date startTime = null;
		Date endTime = null;
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
			if (startSearchTime == null || startSearchTime.length() == 0) {
				startTime = new Date(0);
			} else {
				startTime = sdf.parse(startSearchTime + " 00:00:00");
			}
			if (endSearchTime != null && endSearchTime.length() != 0) {
				endTime = sdf.parse(endSearchTime + " 23:59:59");
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		scmTakeStk.setStartTime(startTime);
		scmTakeStk.setEndTime(endTime);
		List<ScmTakeStkEntity> beanList = scmTakeStkEntityMapper
				.findScmTakeStk(scmTakeStk);
		if (beanList.size() > 0) {
			scmTakeStk.setResults(beanList);
		}
		return scmTakeStk;
	}

	/**
	 * 
	 * @Title: delScmTakeStk
	 * @Description: 删除指定单号的库存盘点单
	 * @param scmTakeStk
	 * @throws RDPException
	 * @date: 2014年9月2日
	 * @return:void
	 */
	public void delScmTakeStk(ScmTakeStkEntity scmTakeStk) throws RDPException {
		scmTakeStkEntityMapper.delScmTakeStk(scmTakeStk);
	}

	/**
	 * 
	 * @Title: doTakeStock
	 * @Description: 库存盘点(开始盘点后就自动添加盘点单）
	 * @param scmTakeStk
	 * @throws RDPException
	 * @date: 2014年9月3日
	 * @return:void
	 */
	public void insertScmTakeStk(ScmTakeStkEntity scmTakeStk)
			throws RDPException {
		scmTakeStkEntityMapper.insertScmTakeStk(scmTakeStk);
	}

	/**
	 * 
	 * @Title: doTakeStock
	 * @Description: 库存盘点-插入盘点清单信息
	 * @param takestkContent
	 * @param fkTskId
	 * @throws RDPException
	 * @date: 2014年9月3日
	 * @return:void
	 */
	public synchronized void doTakeStock(String takestkContent, String fkTskId)
			throws RDPException {
		if (takestkContent != null) {
			String[] takestk = takestkContent.split("[|]");
			for (int i = 0; i < takestk.length; i++) {
				String[] contents = takestk[i].split("_");
				ScmTakestkPrdEntity stp = new ScmTakestkPrdEntity();
				stp.setFkTskId(fkTskId);
				stp.setFkPrdId(contents[0]);
				stp.setStgCnt(Integer.parseInt(contents[1]));
				stp.setStkCnt(Integer.parseInt(contents[2]));
				if (contents.length > 3) {
					stp.setComments(contents[3]);
				}
				ScmTakestkPrdEntity bean = scmTakestkPrdEntityMapper
						.findScmTakestkPrd(stp);
				if (bean == null) {
					scmTakestkPrdEntityMapper.insertScmTakestkPrd(stp);
				} else {
					scmTakestkPrdEntityMapper.updateScmTakestkPrd(stp);
				}
			}
		}
	}

	/**
	 * 根据盘点id 查询已盘点的商品项
	 * @param fkTskId
	 * @return int
	 * @throws RDPException
	 */
	public int findTakestkPrdCount(String fkTskId) throws RDPException {
		ScmTakestkPrdEntity scmTakestkPrd= scmTakestkPrdEntityMapper.findTakestkPrdCount(fkTskId);
		 return scmTakestkPrd.getPrdCnt();
	}
	
	/**
	 * 查询跳转到盘点清单时的数据
	 * @param pkTskId
	 * @return ScmTakeStkEntity
	 * @throws RDPException
	 */
	public ScmTakeStkEntity findScmTakeStkForDetail(String pkTskId)throws RDPException
	{
		return scmTakeStkEntityMapper.findScmTakeStkForDetail(pkTskId);
	}
}
